#! /usr/bin/env node
const fs = require('fs');
const program = require('commander');
const download = require('download-git-repo');
const handlebars = require('handlebars');
const inquirer = require('inquirer');
const ora = require('ora');
const chalk = require('chalk');
const symbols = require('log-symbols');
const pka = require('./package.json')
const child_process = require('child_process');
const util = require('util');
const exec = util.promisify(child_process.exec);
const target = {
  "mini-taro":"https://gitee.com/github-29727412/taro-template.git",
  "react-web":"https://gitee.com/github-29727412/react-web.git",
  "rollup-template":'https://gitee.com/github-29727412/rollup-template.git',
  "webpacket-template":"https://gitee.com/github-29727412/webpack-template.git",
}
program.version(pka.version, '-v, --version')
  .option('init, init <name>', '生成项目命令')
  .command('init <name>')
  .action((name) => {
    if(!fs.existsSync(name)){
      inquirer.prompt([
        {
          type: 'list',
          name: 'template',
          message: '请选择模板',
          choices: [
            "mini-taro",
            "react-web",
            "rollup-template",
            "webpacket-template",
        ],
        },
        {
          name: 'description',
          message: '请输入项目描述'
        },
        {
          name: 'author',
          message: '请输入作者名称'
        },
        { type: "confirm",
          name: 'autoInstall',
          message: '是否自动下载项目依赖'
        }
      ]).then((answers) => {
        // console.log(answers)
        const spinner = ora('download...');
        spinner.start();
        // rollup https://gitee.com/github-29727412/rollup-template.git
        //react-web https://gitee.com/github-29727412/react-web.git
        // webpack https://gitee.com/github-29727412/webpack-template.git
        // taro https://gitee.com/github-29727412/taro-template.git
        
        download(`direct:${target[answers.template]}`, name, {clone: true}, (err) => {
          if(err){
            spinner.fail();
            console.log(chalk.red(err));
          }else{
            const fileName = `${name}/package.json`;
            const meta = {
              name,
              description: answers.description,
              author: answers.author
            }
            if(fs.existsSync(fileName)){
              const content = fs.readFileSync(fileName).toString();
              const result = handlebars.compile(content)(meta);
              fs.writeFileSync(fileName, result);
            }
            if(answers.autoInstall) {
               exec(`yarn --version`).then(res => {
                //  console.log(res)
                 if(res.stdout) {
                  console.log(chalk.green('yarn 下载依赖开始'));
                  exec(`cd ${name} && yarn`).then(() => {
                    spinner.succeed();
                    console.log(chalk.green(symbols.success,'success'));
                    console.log(chalk.green(`cd ${name} && npm run dev:weapp`));
                   }).catch(err => {
                      spinner.fail();
                   });
                 } else {
                  console.log(chalk.green('npm 下载依赖开始'));
                  exec(`cd ${name} && npm install`).then(() => {
                    spinner.succeed();
                    console.log(symbols.success,chalk.green('success'));
                    console.log(chalk.green(`cd ${name} && npm run dev:weapp`));
                   }).catch(err => {
                      spinner.fail();
                   });
                 }
               })
              
            } else {
              spinner.succeed();
              console.log(symbols.success,chalk.green('success'));
              console.log(chalk.green(`cd ${name} && npm install`));
            }
           
          }
        })
      })
    }else{ console.log(symbols.error, chalk.red('项目已存在'));
}
})
program.parse(process.argv);